package com.zhdl.modules.prefiresys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.project.common.service.impl.BaseServiceImpl;
import com.zhdl.modules.prefiresys.entity.DefAreaAlarmRec;
import com.zhdl.modules.prefiresys.entity.Detector;
import com.zhdl.modules.prefiresys.entity.FactoryLocalHost;
import com.zhdl.modules.prefiresys.entity.QFactoryLocalHost;
import com.zhdl.modules.prefiresys.repository.DefAreaAlarmRecRepository;
import com.zhdl.modules.prefiresys.repository.FactoryLocalHostRepository;
import com.zhdl.modules.prefiresys.service.DetectorService;
import com.zhdl.modules.prefiresys.service.FactoryLocalHostService;
import com.zhdl.modules.prefiresys.service.dto.BaseQueryCriteria;
import com.zhdl.modules.prefiresys.web.response.AllHostListDetectorResp;
import com.zhdl.modules.prefiresys.web.response.AllHostListResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
public class FactoryLocalHostServiceImpl extends BaseServiceImpl<String, FactoryLocalHost, FactoryLocalHostRepository> implements FactoryLocalHostService {

    @Autowired
    DetectorService detectorService;

    @Resource
    DefAreaAlarmRecRepository defAreaAlarmRecService;

    @Resource
    FactoryLocalHostRepository factoryLocalHostRepository;

    /**
     * 新增本地接口
     *
     * @param factoryLocalHost
     * @return
     */
    @Override
    public FactoryLocalHost insertHost(FactoryLocalHost factoryLocalHost) {
        FactoryLocalHost newDto = this.insert(factoryLocalHost);

        if (StrUtil.isNotEmpty(factoryLocalHost.getDetectorId1())) {
            Detector detector = new Detector();
            detector.setSeqInHost((byte) 1);
            detectorService.update(detector, factoryLocalHost.getDetectorId1());
        }

        if (StrUtil.isNotEmpty(factoryLocalHost.getDetectorId2())) {
            Detector detector = new Detector();
            detector.setSeqInHost((byte) 2);
            detectorService.update(detector, factoryLocalHost.getDetectorId2());
        }

        if (StrUtil.isNotEmpty(factoryLocalHost.getDetectorId3())) {
            Detector detector = new Detector();
            detector.setSeqInHost((byte) 3);
            detectorService.update(detector, factoryLocalHost.getDetectorId3());
        }

        return newDto;
    }

    /**
     * 修改本地接口
     *
     * @param factoryLocalHost
     * @return
     */
    @Override
    public FactoryLocalHost updateHost(FactoryLocalHost factoryLocalHost) {
        FactoryLocalHost newDto = this.update(factoryLocalHost, factoryLocalHost.getId());

        if (StrUtil.isNotEmpty(factoryLocalHost.getDetectorId1())) {
            Detector detector = new Detector();
            detector.setSeqInHost((byte) 1);
            detectorService.update(detector, factoryLocalHost.getDetectorId1());
        }

        if (StrUtil.isNotEmpty(factoryLocalHost.getDetectorId2())) {
            Detector detector = new Detector();
            detector.setSeqInHost((byte) 2);
            detectorService.update(detector, factoryLocalHost.getDetectorId2());
        }

        if (StrUtil.isNotEmpty(factoryLocalHost.getDetectorId3())) {
            Detector detector = new Detector();
            detector.setSeqInHost((byte) 3);
            detectorService.update(detector, factoryLocalHost.getDetectorId3());
        }

        return newDto;

    }

    @Override
    public List<FactoryLocalHost> getAllTotalHost(){
        List<FactoryLocalHost> all = factoryLocalHostRepository.findAll();
        return all;
    }

    /**
     * 获取装置列表，带设置信息列表
     *
     * @return
     */
    @Override
    public List<AllHostListResp> getAllHostlist() {
        BaseQueryCriteria bc = new BaseQueryCriteria();
        List<FactoryLocalHost> list = findList(bc);

        List<AllHostListResp> outData = new ArrayList<>();
        for (FactoryLocalHost item : list) {
            AllHostListResp detRes = new AllHostListResp();
            BeanUtil.copyProperties(item, detRes, CopyOptions.create().setIgnoreNullValue(true));

            List<AllHostListDetectorResp> detectorList = new ArrayList<>();
            AllHostListDetectorResp detectorResponse;
            Detector detector;
            if (StrUtil.isNotEmpty(item.getDetectorId1())) {
                detectorResponse = new AllHostListDetectorResp();
                detector = detectorService.selectById(item.getDetectorId1());
                BeanUtil.copyProperties(detector, detectorResponse, CopyOptions.create().setIgnoreNullValue(true));
                detectorList.add(detectorResponse);
            }

            if (StrUtil.isNotEmpty(item.getDetectorId2())) {
                detectorResponse = new AllHostListDetectorResp();
                detector = detectorService.selectById(item.getDetectorId2());
                BeanUtil.copyProperties(detector, detectorResponse, CopyOptions.create().setIgnoreNullValue(true));
                detectorList.add(detectorResponse);
            }

            if (StrUtil.isNotEmpty(item.getDetectorId3())) {
                detectorResponse = new AllHostListDetectorResp();
                detector = detectorService.selectById(item.getDetectorId3());
                BeanUtil.copyProperties(detector, detectorResponse, CopyOptions.create().setIgnoreNullValue(true));
                detectorList.add(detectorResponse);
            }

            detRes.setDetectorList(detectorList);
            outData.add(detRes);
        }

        return outData;
    }

    /**
     * 找最近一个月信息
     *
     * @return
     */
    @Override
    public FactoryLocalHost findRecently() {
        List<FactoryLocalHost> list = this.selectAll();
        if (list.size() == 0) return null;

        return list.get(0);
    }

    @Override
    public Integer findAllAlarm(){
        List<DefAreaAlarmRec> all = defAreaAlarmRecService.findAll();
        int count = 0;
        for (DefAreaAlarmRec defAreaAlarmRec : all) {
            if (defAreaAlarmRec.getNextAlarmStatus() == 1) {
                count++;
            }
        }
        return count;
    }

    /**
     * 消音器状态更新
     *
     * @param soundEnable
     */
    @Override
    public void updateSound(boolean soundEnable) {
        QFactoryLocalHost qLocalHost = QFactoryLocalHost.factoryLocalHost;
        jpaQueryFactory().update(qLocalHost).set(qLocalHost.isSound, soundEnable).execute();
    }
}
